package org.activequant.core.domainmodel.index;

import java.io.Serializable;

import org.activequant.core.domainmodel.InstrumentSpecification;
import org.activequant.util.tools.IdentityUtils;

/**
 * Describes index component: which instrument is it and what weight is
 * associated with it.<br/>
 * A final class.IndexComponent implements Serializable, Comparable&lt;IndexComponent&gt;. Holds the following associated variables:
 * <ul>
 * <li>id(Long)</li>
 * <li>index(IndexComposition)</li>
 * <li>spec(InstrumentSpecification)</li>
 * <li>weight(double)</li>
 * </ul>
 * <p>
 * Business key is the index (parent object) and instrument specification.
 * <br>
 * <b>History:</b><br>
 *  - [08.06.2008] Created (Mike Kroutikov)<br>
 *
 *  @author Mike Kroutikov
 *  @author Wessel de Roode
 *  @author SandhuQ
 */
public final class IndexComponent implements Serializable, Comparable<IndexComponent> {
	private static final long serialVersionUID = -6448087914037611075L;
	/**
	 * private Long id;
	 */
	private Long id;
	/**
	 * private IndexComposition index;
	 */
	private IndexComposition index;
	/**
	 * private InstrumentSpecification spec;
	 */
	private InstrumentSpecification spec;
	/**
	 * private double weight;
	 */
	private double weight;
	/**
	 * returns the associated id(Long)
	 * @return
	 */
	public Long getId() {
		return id;
	}
	/**
	 * sets the associated id(Long) with the given val(Long)
	 * @param val
	 */
	public void setId(Long val) {
		id = val;
	}
	/**
	 * returns the associated index(IndexComposition)
	 * @return
	 */
	public IndexComposition getIndex() {
		return index;
	}
	/**
	 * sets the associated index(IndexComposition) with the given val(IndexComposition)
	 * @param val
	 */
	public void setIndex(IndexComposition val) {
		index = val;
	}
	/**
	 * returns the associated spec(InstrumentSpecification)
	 * @return
	 */
	public InstrumentSpecification getInstrumentSpecification() {
		return spec;
	}
	/**
	 * sets the associated spec(InstrumentSpecification) with the given val(InstrumentSpecification)
	 * @param val
	 */
	public void setInstrumentSpecification(InstrumentSpecification val) {
		spec = val;
	}
	/**
	 * returns the associated weight(double)
	 * @return
	 */
	public double getWeight() {
		return weight;
	}
	/**
	 * sets the associated weight(double) with the given val(double)
	 * @param val
	 */
	public void setWeight(double val) {
		weight = val;
	}

	public int compareTo(IndexComponent o) {
		int rc;
		
		if((rc = IdentityUtils.safeCompare(index, o.index)) != 0) {
			return rc;
		}
		if((rc = IdentityUtils.safeCompare(spec, o.spec)) != 0) {
			return rc;
		}
		
		return 0;
	}
	
	@Override
	public int hashCode() {
		return IdentityUtils.safeHashCode(index) + IdentityUtils.safeHashCode(spec);
	}
	
	@Override
	public boolean equals(Object other) {
		return IdentityUtils.equalsTo(this, other);
	}
	/**
	 * returns a String in the form: "${getClass.getSimpleName}{index=${index}, spec=${spec}}"
	 */
	@Override
	public String toString() {
		return getClass().getSimpleName() + "{index=" + index + ", spec=" + spec + "}";
	}
}
